# if/else
$var =2
if($var -eq 1)
{
	Clear-Host
	"If brach"
}
else
{
	Clear-Host
	"else branch"
}

# if elseif else
$var=2
if($var -eq 1)
{
	Clear-Host
	"If -eq 1 branch"
}
elseif($var -eq 2)
{
	Clear-Host
	"elseif -eq 2 branch"
}
else
{
	Clear-Host
	"else branch"
}

# Switch statement for multiple conditions
Clear-Host
$var=42
switch($var)
{
	41 {"Forty One"}
	42 {"Forty Two"}
	43 {"Forty Three"}
	default {"default"}
}

# Will match all lines that match
Clear-Host
$var=42
switch($var)
{
	42 {"Forty Two"}
	"42" {"Forty Two String"}
	default {"default"}
}

# To stop processing once a block is found use break
Clear-Host
$var=42
switch($var)
{
	42 {"Forty Two";break}
	"42" {"Forty Two String";break}
	default {"default"}
}

# Switch works with collections, looping and executing for each match
Clear-Host
switch(3,1,2,42)
{
	1 {"One"}
	2 {"Two"}
	3 {"Three"}
	default {"The default answer"}
}

# String compares are case insensitive by default
Clear-Host
switch("PowerShell")
{
	"powershell" {"lowercase"}
	"POWERSHELL"{"uppercase"}
	"PowerShell"{"mixedcase"}
}

# Use the -casesenstive switch to make it so
Clear-Host
switch -CaseSensitive ("PowerShell")
{
	"powershell" {"lowercase"}
	"POWERSHELL" {"uppercase"}
	"PowerShell" {"mixedcase"}
}

# While
Clear-Host
$i=1
while($i -le 5)
{
	"`$i=$i"
	$i=$i+1
}


# Do
Clear-Host
$i=1
do
{
	"`$i=$i"
	$i++
} while($i -le 5)


# Until
Clear-Host
$i=1
do
{
	"`$i=$i"
	$i++
} until($i -gt 5)

Clear-Host
$i=1
do
{
	"`$i=$i"
	$i++
} while($i -gt 5)

# for loop
Clear-Host
for($f=0;$f -le 5;$f++)
{
	"`$f=$f"
}

# Iterating over a collection 1 by 1
Clear-Host
$array=11,12,13,14,15
for($i=0;$i -lt $array.Length;$i++)
{
	"`$array[$i]="+$array[$i]
}

# foreach works on a collection
Clear-Host
$array=11,12,13,14,15
foreach($item in $array)
{
	"`$item=$item"
}

# foreach works with an array of objects
Clear-Host
Set-Location C:\Code
foreach($item in Get-ChildItem)
{
	$item.Name
}

Clear-Host
Set-Location C:\CODE\LearningPowerShell\Grammars
foreach($file in Get-ChildItem)
{
	if($file.Name -like "*.ps1")
	{
		$file.Name
		break
	}
}


# A script block can exist on its own
# (note, to put multiple commands on a single line use the ;)
{Clear-Host; "PowerShell is cool."}

# Executing only shows the contents of the block, doesn't execute it
# To actually run it, use an ampersand & in front
&{Clear-Host; "Powershell is cool."}

# You can store srcript block inside a variable
$cool={Clear-Host; "Powershell is cool."}
$cool

& $cool
&$cool

# Since scripts can be put in a variable, you can do interesting things
Clear-Host
$cool={"Powershell is cool."; "So is ArcaneCode"}
for($i=0;$i -lt 3;$i++)
{
	&$cool
}

$cool={"Begin";
	"step1";
	"step2"
}

&$cool

